package cc.lucien.listener;

import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

@WebListener
public class HttpSessionListenerImpl implements HttpSessionListener {
    @Override
    public void sessionCreated(HttpSessionEvent se) {
        HttpSession session = se.getSession();
        session.setMaxInactiveInterval(60);
        Integer num = (Integer) session.getServletContext().getAttribute("onLineNum");
        if (num != null) {
            num++;
            session.getServletContext().setAttribute("onLineNum", num);
        } else {
            session.getServletContext().setAttribute("onLineNum", 1);
        }
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent se) {
        Integer num = (Integer) se.getSession().getServletContext().getAttribute("onLineNum");
        num--;
        se.getSession().getServletContext().setAttribute("onLineNum", num);
    }
}
